//广告类型
function ss(index) {
    if ($(".genreBox li").eq(index-1000).attr("class") == '') {
        $(".genreBox li").eq(index-1000).addClass("active").siblings();
        var src = "/Lvscore_User_M/userWeb/static/images/genres_pic" + index + ".png";
        $(".genreBox li img").eq(index-1000).attr("src", src);
    } else if ($(".genreBox li").eq(index-1000).attr("class") == 'active') {
        $(".genreBox li").eq(index-1000).removeClass("active");
        var src1 = "/Lvscore_User_M/userWeb/static/images/genre_pic" + index + ".png";
        $(".genreBox li img").eq(index-1000).attr("src", src1);
    }
}

//兴趣爱好
function kk(index) {
    if ($(".interests li").eq(index-1000).attr("class") == '') {
        $(".interests li").eq(index-1000).addClass("active").siblings();
        var src = "/Lvscore_User_M/userWeb/static/images/interests_pic" + index + ".png";
        $(".interests li img").eq(index-1000).attr("src", src);
    } else if ($(".interests li").eq(index-1000).attr("class") == 'active') {
        $(".interests li").eq(index-1000).removeClass("active");
        var src1 = "/Lvscore_User_M/userWeb/static/images/interest_pic" + index + ".png";
        $(".interests li img").eq(index-1000).attr("src", src1);
    }
}

$(function () {
    $("#lastStep").click(function () {
        history.go(-1);
    });
    $('#keyAD input').blur(function () {
        var this_ = this;
        validateAD_Key(this_);
    });
    $("#later").click(function () {
        if (validateAD_Key()) {
            //广告类型
            var ul = document.getElementById("ad_category_ul").getElementsByTagName("li");
            var ad_category = '';
            for (var i = 0; i < ul.length; i++) {
                if (ul[i].className == 'active') {
                    if (ad_category.length == 0) {
                        ad_category = '|' +  ul[i].value;
                    } else {
                        ad_category = ad_category + '|' + ul[i].value;
                    }
                }
            }

            //广告关键字
            var devId = $("#devId").attr("value");
            var ad_key1 = document.getElementById("ad_key1").value;
            var ad_key2 = document.getElementById("ad_key2").value;
            var ad_key3 = document.getElementById("ad_key3").value;
            var ad_key4 = document.getElementById("ad_key4").value;
            var ad_key5 = document.getElementById("ad_key5").value;
            var ad_key6 = document.getElementById("ad_key6").value;
            var ad_key = '|' + ad_key1 + '|' + ad_key2 + '|' + ad_key3 + '|' + ad_key4 + '|' + ad_key5 + '|' + ad_key6;

            //兴趣
            var ul = document.getElementById("hobby_ul").getElementsByTagName("li");
            var hobby = '';
            for (var i = 0; i < ul.length; i++) {
                if (ul[i].className == 'active') {
                    if (hobby.length == 0) {
                        hobby = '|' + ul[i].value;
                    } else {
                        hobby = hobby + '|' + ul[i].value;
                    }
                }
            }
            var cookie = new sweetCookie();
            var user = JSON.parse(decodeURIComponent(decodeBase64(cookie.getCookie("r_user"), 3)));
            user.adCategory=ad_category;
            user.hobby=hobby;
            user.adKey=ad_key;
            user=JSON.stringify(user);
            ajaxRegisterForDetailedInfo(user);
        }
    });

    $('#ad_category_ul li').click(function () {
        ss($(this).val());
    });

    $('#hobby_ul li').click(function () {
        kk($(this).val());
    });

});

/**
 * 用户注册-填写详细信息
 * @param user
 */
function ajaxRegisterForDetailedInfo(user) {
    var url = "/Lvscore_User_M/register/ajaxRegisterForDetailedInfo";
    $.post(url, {
        user:user
    }, function (data) {
        if (data == '1')
            window.location.href = '/Lvscore_User_M/userWeb/views/register/registerOver.html';
        else
            alertBox('修改失败', " ");
    });
}

/**
 * 校验广告关键字  数字字母20个  汉字10个
 * @param that
 * @returns {boolean}
 */
function validateAD_Key(that) {
    if(that) {
        var info = that.value;
        if (info.replace(/[\u4E00-\u9fa5]/g,"**").length >= 20) {
            alertBox('您输入的长度超出限制', '');
            return false;
        } else {
            return true;
        }
    } else {
        var flag = true;
        $.each($('#keyAD input'),function (i, e) {
            if (!validateAD_Key(e)) {
                flag = false;
                return;
            }
        });
        if (!flag) {
            alertBox('存在不合法的关键字，请检查', '')
        }
        return flag;
    }
}